﻿using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Wise.TimeWarrior.Helpers
{
    /// <summary>
    /// Serialization Helper Class
    /// </summary>
    public static class SerializationHelper
    {
        /// <summary>
        /// Deserialize
        /// </summary>
        /// <typeparam name="T">type of deserialized object</typeparam>
        /// <param name="path">path to serialized file</param>
        /// <param name="fileMode">file mode to acces file , default open</param>
        /// <returns></returns>
        public static T Deserialize<T>(string path, FileMode fileMode = FileMode.Open)
        {
            using (FileStream fs = new FileStream(path, fileMode))
            {
                return ProtoBuf.Serializer.Deserialize<T>(fs);
            }
        }
        /// <summary>
        /// Method serializes data from object 
        /// </summary>
        /// <typeparam name="T">type of serialized object</typeparam>
        /// <param name="path">path to serialized object</param>
        /// <param name="obj">object to serialization</param>
        /// <param name="fileMode">filemode , default create</param>
        public static void Serialize<T>(string path, T obj, FileMode fileMode = FileMode.Create)
        {
            using (FileStream fs = new FileStream(path, fileMode))
            {
                ProtoBuf.Serializer.Serialize(fs,obj);
            }
        }
    }
}
